package xq.gwt.ui.widgets.command;



import xq.gwt.mvc.model.CommandModel;
import xq.gwt.mvc.model.CompositeCommandModel;

import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

public class NavigationCommandListPresenter {
	protected TreeGrid treeGrid;
	protected CommandModel[] navigationCommands;	
	private TreeNode[] treeNodes;
	private String title = "Navigation";
	
	public NavigationCommandListPresenter(){
		treeGrid = new TreeGrid();		
		init();
	}

	public NavigationCommandListPresenter(String title){
		treeGrid = new TreeGrid();		
		this.title = title;
		init();
	}
	
	public NavigationCommandListPresenter(TreeGrid treeGrid){
		this.treeGrid = treeGrid;		
		init();
	}
	
	protected void init(){
		treeGrid.setWidth("30%");
        treeGrid.setShowConnectors(true);
        treeGrid.setShowResizeBar(true);
        treeGrid.setCanSort(false);
	}

	public void setTitle(String title){
		treeGrid.setTitle(title);
	}
	
	public CommandModel[] getNavigationCommands() {
		return navigationCommands;
	}

	public void setNavigationCommands(CommandModel[] navigationCommands) {
		this.navigationCommands = navigationCommands;
		treeNodes = buildTreeNodes(navigationCommands,0);		
		buildNavigationTree();
	}
	
	private TreeNode[] buildTreeNodes(CommandModel[] navigationCommands, int parentId){
		if(navigationCommands == null)
			return null;
		TreeNode[] newTreeNodes = new ObservableTreeNode[navigationCommands.length];
		for (int i = 0; i < navigationCommands.length; i++) {
			CommandModel command = navigationCommands[i];
			ObservableTreeNode commandNode = new ObservableTreeNode();
			commandNode.setName(String.valueOf(parentId) + "." + String.valueOf(i));
			TreeNodeCommandPresenter commandPresenter = new TreeNodeCommandPresenter(commandNode) ;
			commandPresenter.setModel(command);			
			newTreeNodes[i] = commandNode;
			if(command instanceof CompositeCommandModel){
				CompositeCommandModel compositeCommand = (CompositeCommandModel)command;
				TreeNode[] childNodes = buildTreeNodes(compositeCommand.getChildren(), i);
				if(childNodes != null){
					commandNode.setChildren(childNodes);
				}
			}
		}
		
		return newTreeNodes;
	}
	
	
	private void buildNavigationTree(){
		Tree data = new Tree();        
        data.setNameProperty("Name");
        data.setTitleProperty("Title");
        data.setIdField("Name");
        
        data.setShowRoot(false);        
        data.setModelType(TreeModelType.PARENT);
        data.setData(treeNodes);       

        TreeGridField field = new TreeGridField(title);
        field.setCellFormatter(new CellFormatter() {
            public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                return record.getAttribute("title");
            }            
        });
        treeGrid.setFields(field);
        treeGrid.setData(data);
        treeGrid.addCellClickHandler(new CellClickHandler() {

            public void onCellClick(CellClickEvent event) {
            	ObservableTreeNode commandNode = (ObservableTreeNode)event.getRecord();
                commandNode.doClick();
            }
        });        
	}
		
	public Canvas getCanvas(){
		return treeGrid;
	}
}
